package com.wjw.service;

import com.wjw.mapper.DamageSingleTableMapper;
import com.wjw.model.Damage_single_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Damage_single_table_view;
import com.wjw.viewmodel.Damage_type_stable_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DamageSingleTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);


    public List<Damage_single_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);
        try {
            List<Damage_single_table> all = mapper.findAll();
            List<Damage_single_table_view> list=new ArrayList<Damage_single_table_view>();
            for(Damage_single_table e:all){
                Damage_single_table_view k= new Damage_single_table_view();
                k.setDamage_single_density(e.getDamage_single_density());
                k.setDamage_single_id(e.getDamage_single_id());
                k.setDamage_single_value(e.getDamage_single_value());
                k.setDamage_type_sname(mapper.findDamageSById(e.getDamage_type_sid()).getDamage_type_sname());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Damage_single_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Damage_single_table> all = mapper.findByPage(start,limit);
            List<Damage_single_table_view> list=new ArrayList<Damage_single_table_view>();
            for(Damage_single_table e:all){
                Damage_single_table_view k= new Damage_single_table_view();
                k.setDamage_single_density(e.getDamage_single_density());
                k.setDamage_single_id(e.getDamage_single_id());
                k.setDamage_single_value(e.getDamage_single_value());
                k.setDamage_type_sname(mapper.findDamageSById(e.getDamage_type_sid()).getDamage_type_sname());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Damage_single_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);
        try {
            Damage_single_table k= new Damage_single_table();
            k.setDamage_single_density(e.getDamage_single_density());
            k.setDamage_single_id(e.getDamage_single_id());
            k.setDamage_single_value(e.getDamage_single_value());
            k.setDamage_type_sid(mapper.findDamageSByName(e.getDamage_type_sname()).getDamage_type_sid());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Damage_single_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageSingleTableMapper mapper=sqlsession.getMapper(DamageSingleTableMapper.class);
        try {
            Damage_single_table k= new Damage_single_table();
            k.setDamage_single_density(e.getDamage_single_density());
            k.setDamage_single_id(e.getDamage_single_id());
            k.setDamage_single_value(e.getDamage_single_value());
            k.setDamage_type_sid(mapper.findDamageSByName(e.getDamage_type_sname()).getDamage_type_sid());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
